/**
 * 抓取网站信息
 * */

const puppeteer = require('puppeteer')
const fs = require('fs')

const reptile = async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()
  await page.goto('https://search.jd.com/Search?keyword=javascript')

  const result = await page.evaluate(() => {
    let elems = document.querySelectorAll('.gl-item')
    const data = [...elems].map(dom => {
      return {
        name: dom.querySelector('.p-name em').innerText,
        description: dom.querySelector('.p-name i').innerText,
        price: dom.querySelector('.p-price').innerText,
        shop: dom.querySelector('.p-shopnum').innerText,
        url: dom.querySelector('.p-img a').href
      }
    })
    return data
  })

  await browser.close()
  return result
}

reptile().then(res => {
  fs.writeFile(
    `${__dirname}/data/javascriptBooks.json`,
    JSON.stringify(res),
    err => {
      if (err) throw err
      console.log('file has saved')
    }
  )
})
